﻿using System.IO;
using System.Windows;
using System.Windows.Forms;
using AttachedCommands.ACB;
using Snippet2Vsi.Classes;
using Snippet2Vsi.Interface;
using Snippet2Vsi.Properties;
using Snippet2Vsi.Views;
using SnippetEditor;
using MessageBox = System.Windows.MessageBox;
using SaveFileDialog = Microsoft.Win32.SaveFileDialog;

namespace Snippet2Vsi.ViewModels
{
    public class Vsi2SnippetConverterViewModel:ViewModelBase
    {
        #region Constructor

        public Vsi2SnippetConverterViewModel(IVsi2SnippetConverter view)
        {
            _view = view;
        }

        #endregion

        #region Members


        private readonly IVsi2SnippetConverter _view;
        private RelayCommand _browseVsiCommand;
        private RelayCommand _browseExtractCommand;
        private RelayCommand _convertCommand;

        #endregion

        #region Commands


        public RelayCommand ConvertCommand
        {
            get { return _convertCommand ?? (_convertCommand = new RelayCommand(ConvertCommandHandler)); }
            set { _convertCommand = value; }

        }



        public RelayCommand BrowseExtractCommand
        {
            get { return _browseExtractCommand ?? (_browseVsiCommand = new RelayCommand(BrowseExtractCommandHandler)); }
            set { _browseExtractCommand = value; }

        }

        public RelayCommand BrowseVsiCommand
        {
            get { return _browseVsiCommand ?? (_browseVsiCommand = new RelayCommand(BrowseVsiCommandHandler)); }
            set { _browseVsiCommand = value; }

        }

        #endregion

        #region Handlers

        private void BrowseExtractCommandHandler(object obj)
        {
           TryCatch.BeginTryCatch(Browse);
        }

        

        private void BrowseVsiCommandHandler(object obj)
        {
            TryCatch.BeginTryCatch(BrwseVsi);
        }

       

        private void ConvertCommandHandler(object obj)
        {
            TryCatch.BeginTryCatch(Convert);
        }

        #endregion

        #region Methods

        private void Convert()
        {
            var error = ValidateForm();
            if (!string.IsNullOrEmpty(error))
            {
                MessageBox.Show(error, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                var result = VsiConverter.SaveAsSnippet(_view.VsiTextBox.Text, _view.ExtractTextBox.Text);
                if (result)
                {
                    MessageBox.Show("vsi converted to snippet file successfully", "Success", MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                }
                else
                    MessageBox.Show(
                        "There was some error while converting vsi files to snippet files.Kindly check logs.",
                        "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void BrwseVsi()
        {
            var saveFileDialog = new OpenFileDialog
            {
                Filter = Resources.Vsi2SnippetConverterViewModel_BrwseVsi_Vsi___vsi____vsi
            };
            saveFileDialog.ShowDialog();
            if (!string.IsNullOrEmpty(saveFileDialog.FileName))
            {
                _view.VsiTextBox.Text = saveFileDialog.FileName;
            }
        }
        private void Browse()
        {
            

            var openFileDailog = new FolderBrowserDialog();
            openFileDailog.ShowDialog();
            if (!string.IsNullOrEmpty(openFileDailog.SelectedPath))
            {
                _view.ExtractTextBox.Text = openFileDailog.SelectedPath;
            }
        }

        public string ValidateForm()
        {
            var error="";

            if (string.IsNullOrEmpty(_view.VsiTextBox.Text))
            {
                error = "Vsi location cannot be empty";
                _view.VsiTextBox.Focus();
            }
            else if (string.IsNullOrEmpty(_view.ExtractTextBox.Text))
            {
                error = "Extract location cannot be empty";
                _view.ExtractTextBox.Focus();
            }
          
            else if (_view.VsiTextBox.Text.LastIndexOf(".vsi") != _view.VsiTextBox.Text.Length-4)
            {
                error = "Vsi location should end with vsi extension";
                _view.VsiTextBox.Focus();
            }

            return error;
        }

        #endregion
    }
}
